﻿@page "/SysDevice/Edit/{id}"
@using SelfService.ViewModel.SysDeviceVMs;
@using System.Net
@using System.IO
@inherits BasePage

<ValidateForm @ref="VForm" Model="@_model" OnValidSubmit="@Submit">
    <Row ItemsPerRow="ItemsPerRow.Two" RowType="RowType.Normal">
        <BootstrapInput @bind-Value="@_model.Entity.Name"  />
        <BootstrapInput @bind-Value="@_model.Entity.Model"  />
        <BootstrapInput @bind-Value="@_model.Entity.Description"  />
        <Switch @bind-Value="@_model.Entity.IsSwallow"/>
        <Select @bind-Value="@_model.Entity.Mode"  PlaceHolder="@WtmBlazor.Localizer["Sys.PleaseSelect"]"></Select>
        <Select @bind-Value="@_model.Entity.GroupId" Items="@_allSysDeviceGroups" PlaceHolder="@WtmBlazor.Localizer["Sys.PleaseSelect"]"></Select>
        <BootstrapInputNumber @bind-Value="@_model.Entity.Port"  />
        <BootstrapInput @bind-Value="@_model.Entity.DllName"  />
        <InputUpload TValue="string" @bind-Value="@_model.DllZipName" ShowLabel="true" OnChange="@OnFileChange"></InputUpload>
        <BootstrapInput @bind-Value="@_model.Entity.DllClass"  />
        <BootstrapInput @bind-Value="@_model.Entity.DllWorkSpace"  />
        <Select @bind-Value="@_model.Entity.IsEnable"  PlaceHolder="@WtmBlazor.Localizer["Sys.PleaseSelect"]"></Select>
    </Row>
    <div class="modal-footer table-modal-footer">
        <Button Color="Color.Warning" Icon="fa fa-close" Text="@WtmBlazor.Localizer["Sys.Close"]" OnClick="OnClose"></Button>
        <Button Color="Color.Primary" ButtonType="ButtonType.Submit" Icon="fa fa-save" Text="@WtmBlazor.Localizer["Sys.Edit"]" IsAsync="true"></Button>
    </div>
</ValidateForm>

@code {

    private SysDeviceVm _model = null;
    private ValidateForm VForm { get; set; }
    [Parameter]
    public string Id { get; set; }
    private InputUpload<UploadFile> Upload { get; set; }
    private List<SelectedItem> _allSysDeviceGroups = new List<SelectedItem>();

    protected override async Task OnInitializedAsync()
    {
        _allSysDeviceGroups = await WtmBlazor.Api.CallItemsApi("/api/SysDevice/GetSysDeviceGroups", placeholder: WtmBlazor.Localizer["Sys.PleaseSelect"]);
        var rv = await WtmBlazor.Api.CallAPI<SysDeviceVm>($"/api/SysDevice/{Id}");
        _model = rv.Data;
        await base.OnInitializedAsync();
    }

    private async Task<byte[]> GetFileData(IBrowserFile file)
    {
        await using var fileStream = file.OpenReadStream(WtmBlazor.ConfigInfo.FileUploadOptions.UploadLimit);
        await using var memoryStream = new MemoryStream();
        await fileStream.CopyToAsync(memoryStream);
        byte[] rv = memoryStream.ToArray();
        await memoryStream.DisposeAsync();
        await fileStream.DisposeAsync();
        return rv;
    }

    private async Task OnFileChange(UploadFile file)
    {
        var isSuc = false;
        if (file != null && file.File != null)
        {
            var format = file.File.ContentType;
            if (!format.Contains("zip"))
            {
                await WtmBlazor.Toast.Error("上传失败, 仅支持ZIP文件");
                return;
            }
            var fileData = await GetFileData(file.File);
            var result = await WtmBlazor.Api.CallAPI<DynamicData>(
                "/api/_file/Upload", 
                HttpMethodEnum.POST,
                null,
                fileData,
                file.File.Name
                );
            if (result.StatusCode == HttpStatusCode.OK)
            {
                if (Guid.TryParse(result.Data.Fields["Id"].ToString(), out var id))
                {
                    _model.Entity.DllZipId = id;
                    isSuc = true;
                }
            }
        }
        if(!isSuc)await WtmBlazor.Toast.Error(content: WtmBlazor.Localizer["Sys.UploadFailed"]);
    }



    private async Task Submit(EditContext context)
    {
        await PostsForm(VForm, $"/api/SysDevice/edit", (s) => "Sys.OprationSuccess", method: HttpMethodEnum.PUT);
    }

    public void OnClose()
    {
        CloseDialog();
    }
}
